package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.service.ICommodityInfoService;
import com.ztgf.commodity.models.vo.CommodityPriceVO;
import com.ztgf.shopping.models.bo.epg.EpgAggregationBannerBO;
import com.ztgf.shopping.models.bo.epg.EpgAggregationCategoryCommodityRelationBO;
import com.ztgf.shopping.models.bo.epg.EpgShopSectionCommodityRelationBO;
import com.ztgf.shopping.models.constant.ShoppingConstant;
import com.ztgf.shopping.models.dto.epg.EpgShopSectionCommodityRelationDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.vo.epg.EpgAggregationCategoryCommodityRelationVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.shopping.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.mapper.epg.EpgAggregationCategoryCommodityRelationMapper;
import com.ztgf.shopping.models.service.epg.IEpgAggregationCategoryCommodityRelationService;
import com.ztgf.shopping.models.bo.epg.EpgAggregationCategoryCommodityRelationSaveBO;
import com.ztgf.shopping.models.dto.epg.EpgAggregationCategoryCommodityRelationDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * 聚合页分类商品控制器
 *
 * @author YUE
 * @Date 2020-06-04 15:38:23
 */
@DubboService
public class EpgAggregationCategoryCommodityRelationServiceImpl extends ServiceImpl<EpgAggregationCategoryCommodityRelationMapper, EpgAggregationCategoryCommodityRelationDTO> implements IEpgAggregationCategoryCommodityRelationService{

    @Resource
    private YmlConfig ymlConfig;

    @DubboReference
    private ICommodityInfoService iCommodityInfoService;

    /**
     * 根据分类id删除商品
     * @param categoryId
     * @return
     */
    public boolean deleteByCategoryId(Long categoryId){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("category_id", categoryId);
        return delete(wrapper);
    }

    @Override
    public boolean add(EpgAggregationCategoryCommodityRelationSaveBO saveBO) {
        EpgAggregationCategoryCommodityRelationDTO dto = new EpgAggregationCategoryCommodityRelationDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    /**
     * 添加必要数据
     * @param epgAggregationCategoryCommodityRelationBO
     * @return
     */
    private EpgAggregationCategoryCommodityRelationBO addMustData(EpgAggregationCategoryCommodityRelationBO epgAggregationCategoryCommodityRelationBO){
        epgAggregationCategoryCommodityRelationBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgAggregationCategoryCommodityRelationBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
        return epgAggregationCategoryCommodityRelationBO;
    }



    /**
     * 根据条件选择查询 分类商品
     *
     * @param epgAggregationCategoryCommodityRelationBO
     * @return
     */
    @Override
    public List<EpgAggregationCategoryCommodityRelationVO> getBySelectSectionCommodityRelationInfo(EpgAggregationCategoryCommodityRelationBO epgAggregationCategoryCommodityRelationBO){
        List<EpgAggregationCategoryCommodityRelationVO> epgAggregationCategoryCommodityRelationVOList = new ArrayList<>();
        epgAggregationCategoryCommodityRelationBO = addMustData(epgAggregationCategoryCommodityRelationBO);
        Wrapper<EpgAggregationCategoryCommodityRelationDTO> wrapper = getBySelectCommodityRelationDTO(epgAggregationCategoryCommodityRelationBO);
        List<EpgAggregationCategoryCommodityRelationDTO> epgAggregationCategoryCommodityRelationDTOList = baseMapper.selectList( wrapper);
        for (EpgAggregationCategoryCommodityRelationDTO epgSectionCommodityRelationDTO : epgAggregationCategoryCommodityRelationDTOList) {
            CommodityPriceVO commodityPriceVO = iCommodityInfoService.getByIdOtherInterface(epgSectionCommodityRelationDTO.getCommodityId());//商品
            EpgAggregationCategoryCommodityRelationVO epgAggregationCategoryCommodityRelationVO = new EpgAggregationCategoryCommodityRelationVO();
            BeanUtils.copyProperties(epgSectionCommodityRelationDTO,epgAggregationCategoryCommodityRelationVO);
            epgAggregationCategoryCommodityRelationVO.setCommodityPriceVO(commodityPriceVO);
            epgAggregationCategoryCommodityRelationVOList.add(epgAggregationCategoryCommodityRelationVO);
        }
        return epgAggregationCategoryCommodityRelationVOList;
    }


    /**
     * 根据必要查询
     * @param epgAggregationCategoryCommodityRelationBO
     * @return
     */
    private Wrapper<EpgAggregationCategoryCommodityRelationDTO> getBySelectCommodityRelationDTO(EpgAggregationCategoryCommodityRelationBO epgAggregationCategoryCommodityRelationBO){
        Wrapper<EpgAggregationCategoryCommodityRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("is_publish", epgAggregationCategoryCommodityRelationBO.getIsPublish());
        wrapper.eq("category_id", epgAggregationCategoryCommodityRelationBO.getCategoryId());
        if(ShoppingCodeEnum.SORT_DESC.getCode() == epgAggregationCategoryCommodityRelationBO.getSort())
            wrapper.orderBy("sort", false);
        else
            wrapper.orderBy("sort", true);
        return wrapper;
    }
}
